package cc.rengu.igas.bsps.core.service.trans;

import cc.rengu.igas.bsps.common.dao.BspsIndustryInfoMapper;
import cc.rengu.igas.bsps.common.dao.BspsManagerInfoMapper;
import cc.rengu.igas.bsps.common.dao.BspsMchntBaseInfoMapper;
import cc.rengu.igas.bsps.common.dao.BspsMchntSignInfoMapper;
import cc.rengu.igas.bsps.common.dao.impl.BspsIndustryInfoMapperImpl;
import cc.rengu.igas.bsps.common.dao.impl.BspsManagerInfoMapperImpl;
import cc.rengu.igas.bsps.common.dao.impl.BspsMchntBaseInfoMapperImpl;
import cc.rengu.igas.bsps.common.dao.impl.BspsMchntSignInfoMapperImpl;
import cc.rengu.igas.bsps.common.entity.IndustryInfo;
import cc.rengu.igas.bsps.common.entity.ManagerInfo;
import cc.rengu.igas.bsps.common.entity.MchntBaseInfo;
import cc.rengu.igas.bsps.common.entity.MchntSignInfo;
import cc.rengu.igas.bsps.common.enums.RespCodeEnum;
import cc.rengu.igas.bsps.common.enums.RoleTypeEnum;
import cc.rengu.igas.bsps.core.service.base.ManageService;
import cc.rengu.igas.bsps.facade.bean.ExpandMchntDetailInfoBean;
import cc.rengu.igas.bsps.facade.request.ExpandMchntDetailListQueryRequest;
import cc.rengu.igas.bsps.facade.response.ExpandMchntDetailListQueryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import org.springframework.util.CollectionUtils;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 拓展商户明细列表查询
 * Created by 王成 on 2020/3/25
 */
public class ExpandMchntDetailListQueryService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        ExpandMchntDetailListQueryRequest expandMchntDetailListQueryRequest = new ExpandMchntDetailListQueryRequest();
        ConvertUtil.convertOutput(expandMchntDetailListQueryRequest);
        return expandMchntDetailListQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        ExpandMchntDetailListQueryRequest expandMchntDetailListQueryRequest = (ExpandMchntDetailListQueryRequest) request;
        ExpandMchntDetailListQueryResponse expandMchntDetailListQueryResponse = new ExpandMchntDetailListQueryResponse();

        String userId = expandMchntDetailListQueryRequest.getUserId();
        String queryType = expandMchntDetailListQueryRequest.getQueryType();
        String queryId = expandMchntDetailListQueryRequest.getQueryId();
        String startDate = expandMchntDetailListQueryRequest.getStartDate();
        String endDate = expandMchntDetailListQueryRequest.getEndDate();
        String instId = expandMchntDetailListQueryRequest.getHeader().getInstId();
        int pageIndex = expandMchntDetailListQueryRequest.getPageIndex();
        int pageSize = expandMchntDetailListQueryRequest.getPageSize();

        /* 参数合法性校验 */
        if (StringUtil.isEmptyOrNull(userId) || StringUtil.isEmptyOrNull(queryType) || StringUtil.isEmptyOrNull(queryId) || StringUtil.isEmptyOrNull(startDate) || StringUtil.isEmptyOrNull(endDate)) {
            rglog.error("用户编号<{}>,查询类型<{}>,查询主体<{}>,开始日期<{}>,截止日期<{}>不能为空!", userId, queryType, queryId, startDate, endDate);
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        /* 根据查询类型及查询主体查询商户信息 */
        PageResult pageResult;
        PageInfo pageInfo = new PageInfo();
        pageInfo.setPageIndex(pageIndex);
        pageInfo.setPageSize(pageSize);
        BspsMchntBaseInfoMapper bspsMchntBaseInfoMapper = new BspsMchntBaseInfoMapperImpl();
        if (RoleTypeEnum.AGENT_MCHNT.getRoleType().equals(queryType)) {
            pageResult = bspsMchntBaseInfoMapper.selectBspsMchntBaseInfoByManagerIdAndDate(instId, queryId, startDate, endDate, pageInfo);
        } else {
            rglog.error("非法操作，不支持的操作类型:<{}>", expandMchntDetailListQueryRequest.getQueryType());
            throw new BizException(RespCodeEnum.ILLEGAL_OPERATION.getRespCode(), RespCodeEnum.ILLEGAL_OPERATION.getRespDesc());
        }
        BizResponse<ExpandMchntDetailListQueryResponse> bizResponse = new BizResponse();
        if (null == pageResult || CollectionUtils.isEmpty(pageResult.getResult())) {
            expandMchntDetailListQueryResponse.setTotalPage(0);
            expandMchntDetailListQueryResponse.setTotalRows(0);
        } else {
            expandMchntDetailListQueryResponse.setTotalRows(pageInfo.getTotalRows());
            expandMchntDetailListQueryResponse.setTotalPage(pageInfo.getTotalPage());
            expandMchntDetailListQueryResponse.setPageIndex(pageInfo.getPageIndex());
            expandMchntDetailListQueryResponse.setPageSize(pageInfo.getPageSize());
            List<MchntBaseInfo> mchntBaseInfoList = pageResult.getResult().stream().map(item -> (MchntBaseInfo) item).collect(Collectors.toList());
            List<ExpandMchntDetailInfoBean> expandMchntDetailInfos = new ArrayList<>();
            for (MchntBaseInfo mchntBaseInfo : mchntBaseInfoList) {
                ExpandMchntDetailInfoBean expandMchntDetailInfo = new ExpandMchntDetailInfoBean();
                BeanUtil.beanCopy(mchntBaseInfo, expandMchntDetailInfo);
                BspsMchntSignInfoMapper bspsMchntSignInfoMapper = new BspsMchntSignInfoMapperImpl();
                MchntSignInfo mchntSignInfo = bspsMchntSignInfoMapper.selectBspsMchntSignInfoByPrimaryKey(mchntBaseInfo.getInstId(), mchntBaseInfo.getMchntNo());
                if (null != mchntSignInfo) {
                    expandMchntDetailInfo.setIndustryType(mchntBaseInfo.getIndustryType());
                    BspsIndustryInfoMapper bspsIndustryInfoMapper = new BspsIndustryInfoMapperImpl();
                    IndustryInfo industryInfo = bspsIndustryInfoMapper.selectBspsIndustryInfoByIndustryType(mchntBaseInfo.getIndustryType());
                    if (null != industryInfo) {
                        expandMchntDetailInfo.setIndustryName(industryInfo.getIndustryName());
                    }
                }
                BspsManagerInfoMapper bspsManagerInfoMapper = new BspsManagerInfoMapperImpl();
                ManagerInfo managerInfo = bspsManagerInfoMapper.selectBspsManagerInfoByPrimaryKey(mchntBaseInfo.getInstId(), mchntBaseInfo.getSignInstId(), mchntBaseInfo.getManagerId());
                if (null != managerInfo) {
                    expandMchntDetailInfo.setManagerName(managerInfo.getManagerName());
                    expandMchntDetailInfo.setManagerPhone(managerInfo.getManagerPhone());
                }
                expandMchntDetailInfos.add(expandMchntDetailInfo);
            }
            expandMchntDetailListQueryResponse.setExpandMchntDetailInfoList(expandMchntDetailInfos);
        }
        /* 组装应答报文 */

        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(expandMchntDetailListQueryResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        ExpandMchntDetailListQueryResponse expandMchntDetailListQueryResponse = (ExpandMchntDetailListQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(expandMchntDetailListQueryResponse);
    }

    /**
     * 将数据库查询结果转换成接口应答结果
     *
     * @param mchntBaseInfo          数据库查询结果
     * @param expandMchntDetailInfos 接口应答结果
     */
    private void convExpandMchntDetailInfo(MchntBaseInfo mchntBaseInfo, List<ExpandMchntDetailInfoBean> expandMchntDetailInfos) {
        ExpandMchntDetailInfoBean expandMchntDetailInfo = new ExpandMchntDetailInfoBean();
        BeanUtil.beanCopy(mchntBaseInfo, expandMchntDetailInfo);
        try {
            BspsMchntSignInfoMapper bspsMchntSignInfoMapper = new BspsMchntSignInfoMapperImpl();
            MchntSignInfo mchntSignInfo = bspsMchntSignInfoMapper.selectBspsMchntSignInfoByPrimaryKey(mchntBaseInfo.getInstId(), mchntBaseInfo.getMchntNo());
            if (null != mchntSignInfo) {
                expandMchntDetailInfo.setIndustryType(mchntBaseInfo.getIndustryType());
                BspsIndustryInfoMapper bspsIndustryInfoMapper = new BspsIndustryInfoMapperImpl();
                IndustryInfo industryInfo = bspsIndustryInfoMapper.selectBspsIndustryInfoByIndustryType(mchntBaseInfo.getIndustryType());
                if (null != industryInfo) {
                    expandMchntDetailInfo.setIndustryName(industryInfo.getIndustryName());
                }
            }
            BspsManagerInfoMapper bspsManagerInfoMapper = new BspsManagerInfoMapperImpl();
            ManagerInfo managerInfo = bspsManagerInfoMapper.selectBspsManagerInfoByPrimaryKey(mchntBaseInfo.getInstId(), mchntBaseInfo.getSignInstId(), mchntBaseInfo.getManagerId());
            if (null != managerInfo) {
                expandMchntDetailInfo.setManagerName(managerInfo.getManagerName());
                expandMchntDetailInfo.setManagerPhone(managerInfo.getManagerPhone());
            }
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("拓展商户明细查询数据转换异常:{}", byteArrayOutputStream.toString());
        }
        expandMchntDetailInfos.add(expandMchntDetailInfo);
    }
}
